/*
 * Author: Wojciech Walczak <wojtek.gminick.walczak@gmail.com>
 * Copyright 2008 Wojciech Walczak
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


//#include <python2.5/Python.h>

#include "Python.h"

#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <utmp.h>

static PyObject *UtmpError;


PyDoc_STRVAR(getutent_doc, "getutent() -> returns list of dictionaries\n\n\
Each dictionary contains data as in utmp struct defined in utmp(5) which is:\n\
      \"ut_type\"    => type of login\n\
      \"ut_pid\"     => PID of login process\n\
      \"ut_line\"    => device name of tty - \"/dev/\"\n\
      \"ut_id\"      => init id or abbrev. ttyname\n\
      \"ut_user\"    => user name\n\
      \"ut_host\"    => hostname for remote login\n\
      \"ut_exit\"    => { \"e_termination\" -> process termination status,\n\
                               \"e_exit\" -> process exit status }\n\
      \"ut_session\" => session ID\n\
      \"ut_tv\"      => time entry was made: { \"tv_sec\"  -> seconds,\n\
                                             \"tv_usec\" -> microseconds }\n");

PyObject *utmp_getutent(PyObject *self, PyObject *args)
{
   struct utmp *u=malloc(sizeof(struct utmp));
   PyObject *list = PyList_New(0);
   PyObject *o, *ut_exit, *ut_tv;

   if(!PyArg_ParseTuple(args, ""))
      return NULL;

   setutent();
   while(1) {
      u = getutent();
      if(!u) break;
      if(u->ut_type != USER_PROCESS) continue;

      ut_exit = Py_BuildValue("{s:h,s:h}",
                          "e_termination", u->ut_exit.e_termination,
                                 "e_exit", u->ut_exit.e_exit);

      if(ut_exit == NULL)
         return NULL;

      ut_tv = Py_BuildValue("{s:i,s:i}",
                               "tv_sec", u->ut_tv.tv_sec,
                              "tv_usec", u->ut_tv.tv_usec);

      if(ut_tv == NULL)
         return NULL;

      o = Py_BuildValue("{s:h,s:i,s:s,s:s,s:s,s:s,s:O,s:i,s:O}",
                                     "ut_type", u->ut_type,
                                     "ut_pid",  u->ut_pid,
                                     "ut_line", u->ut_line, 
                                     "ut_id",   u->ut_id,
                                     "ut_user", u->ut_user, 
                                     "ut_host", u->ut_host,
                                     "ut_exit", ut_exit,
                                     "ut_session", u->ut_session,
                                     "ut_tv", ut_tv);

      if(o == NULL)
         return NULL;

      PyList_Append(list, o);
   }
   return list;
}

static PyMethodDef utmpMethods[] = {
    {"getutent", utmp_getutent, METH_VARARGS, getutent_doc},
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC
initutmp(void)
{
   PyObject *m, *d;

   m = Py_InitModule("utmp", utmpMethods);
   d = PyModule_GetDict(m);
   UtmpError = PyErr_NewException("utmp.error", NULL, NULL);
   Py_INCREF(UtmpError);
   PyModule_AddObject(m, "error", UtmpError);
}
